package yannuo.service.impl;


import yannuo.dao.EmpDao;
import yannuo.dao.impl.EmpDaoImpl;
import yannuo.pojo.Emp;
import yannuo.service.EmpService;
import yannuo.util.PageUtils;

import java.util.List;

public class EmpServiceImpl implements EmpService {
    @Override
    public PageUtils listEmp(int pageNum, int pageSize) {
        //分页工具类属性值 进行初始化
        int totalRow = 0;
        int totalPageNum = 0;
        int preNum = 0;
        int nextNum = 0;
        int beginNum = 0;

        PageUtils pageUtils = new PageUtils();
        EmpDao ed = new EmpDaoImpl();
        int empCount = ed.getEmpCount();
        //计算总行数
        totalRow = empCount;
        if(totalRow == 0){
            return pageUtils;
        }
        //计算总页数
        if(totalRow%pageSize==0){
            totalPageNum = totalRow/pageSize;
        }else{
            totalPageNum = totalRow/pageSize+1;
        }
        //总页数 校验
        if(pageNum<=0){
            pageNum = 1;
        }

        if(pageNum>totalPageNum){
            pageNum = totalPageNum;
        }
        //设置上一页和下一页
        preNum = pageNum;
        nextNum = pageNum;
        if(pageNum>1){
            preNum--;
        }
        if(pageNum<totalPageNum){
            nextNum++;
        }
        //开始那一行
        beginNum = (pageNum-1)*pageSize;

        //从dao层获取分页数据
        List<Emp> emps = ed.pageQueryAllEmps(beginNum, pageSize);

        pageUtils.setTotalRow(totalRow);
        pageUtils.setTotalPageNum(totalPageNum);
        pageUtils.setPreNum(preNum);
        pageUtils.setNextNum(nextNum);
        pageUtils.setPageSize(pageSize);
        pageUtils.setList(emps);
        pageUtils.setBeginNum(beginNum);
        pageUtils.setPageNum(pageNum);
        return pageUtils;
    }
}
